import router from '~/router'
import {getToken} from "~/composables/auth.js";
// 引入utils(通知、加载)
import {hideFullLoading, notify, showFullLoading} from "~/composables/utils.js";
import store from "~/store/index.js";


//全局前置守卫，权限校验
router.beforeEach(async (to, from, next) => {
    // 显示loading
    showFullLoading()
    const token = getToken();
    console.log(token)
    // 没有登录，强制跳转
    if (!token && to.path !== '/login') {
        notify("请先登录", "error");
        return next('/login');
    }
    // 防止重复登录
    if (token && to.path === '/login') {
        notify("已登录，请勿重复登录", "error");
        return next(from.path ? from.path : '/');
    }

    // 用户登录了，自动获取登录信息，并存储在vuex
    if (token) {
        await store.dispatch("getInfo")
    }
    // 设置标题
    document.title = (to.meta.title ? to.meta.title : "") + " - 秒购后台管理平台";
    next();
})
//全局后置守卫
router.afterEach((to, from) => {
    // 隐藏loading
    hideFullLoading()
})